import Vue from 'vue'
import Router from 'vue-router'
import store from '../../store/index'
import JSBridge from '../common/JSBridge'
Vue.use(Router)
const jsBridge = JSBridge.getInstance()

const routes = [{
  name: 'debug',
  component: () => import('@/view/debug')
},
{
  name: 'debug/fingerprint',
  component: () => import('@/view/debug/fingerprint')
},
{
  name: 'error',
  component: () => import('@/view/page/error'),
  meta: {
    title: '系统提示'
  }
},
{
  name: 'home',
  component: () => import('@/view/home')
},
{
  name: 'discover',
  component: () => import('@/view/discover'),
  meta: {
    title: '发现'
  }
},
{
  name: 'login',
  component: () => import('@/view/passport/login'),
  meta: {
    title: '登录'
  }
},
{
  name: 'article/default/:id',
  component: () => import('@/view/article/default'),
  meta: {
    title: '动态详情'
  }
},
{
  name: 'notice',
  component: () => import('@/view/notice'),
  meta: {
    title: '消息通知'
  }
},
{
  name: 'goods/:id',
  component: () => import('@/view/goods/index')
},
{
  name: 'balance',
  component: () => import('@/view/order/balance'),
  meta: {
    title: '订单详情'
  }
},
{
  name: 'order/list/:type',
  component: () => import('@/view/order/list'),
  meta: {
    title: '我的订单'
  }
},
{
  name: 'order/view/:id',
  component: () => import('@/view/order/view'),
  meta: {
    title: '订单详情'
  }
},
{
  name: 'user/address',
  component: () => import('@/view/user/address'),
  meta: {
    title: '管理收货地址'
  }
},
{
  name: 'user/address/:id',
  component: () => import('@/view/user/addressEdit'),
  meta: {
    title: '修改收件地址'
  }
},
{
  name: 'commission',
  component: () => import('@/view/commission'),
  meta: {
    title: '会员中心',
    container: 'bg-white'
  }
},
{
  name: 'commission/withdraw',
  component: () => import('@/view/commission/withdraw'),
  meta: {
    title: '佣金提现'
  }
},
{
  name: 'commission/brokerage',
  component: () => import('@/view/commission/brokerage'),
  meta: {
    title: '分销佣金'
  }
},
{
  name: 'commission/downer',
  component: () => import('@/view/commission/downer'),
  meta: {
    title: '我的关注'
  }
},
{
  name: 'commission/rank',
  component: () => import('@/view/commission/rank'),
  meta: {
    title: '佣金排名'
  }
},
{
  name: 'commission/order',
  component: () => import('@/view/commission/order'),
  meta: {
    title: '分销订单'
  }
},
{
  name: 'commission/history',
  component: () => import('@/view/commission/history'),
  meta: {
    title: '提现记录'
  }
},
{
  name: 'commission/qrcode',
  component: () => import('@/view/commission/qrcode'),
  meta: {
    title: '推广二维码'
  }
},
{
  name: 'channel/:name',
  component: () => import('@/view/channel/channel'),
  meta: {
    keepAlive: true
  }
},

{
  name: 'member',
  component: () => import('@/view/member'),
  meta: {
    title: '会员服务'
  }
},
{
  name: 'member/service',
  component: () => import('@/view/member/service'),
  meta: {
    title: '会员服务'
  }
},
{
  name: 'member/apply/:id',
  component: () => import('@/view/member/apply'),
  meta: {
    title: '会员服务申请'
  }
},
{
  name: 'member/data',
  component: () => import('@/view/member/data'),
  meta: {
    title: '会员申请信息'
  }
},
{
  name: 'spread/register',
  component: () => import('@/view/spread/register'),
  meta: {
    title: '优越定制'
  }
},
{
  name: 'spread/success',
  component: () => import('@/view/spread/success'),
  meta: {
    title: '优越定制'
  }
},
{
  name: 'help',
  component: () => import('@/view/help'),
  meta: {
    title: '帮助与反馈'
  }
},
{
  name: 'help/cate/:id',
  component: () => import('@/view/help/cate'),
  meta: {
    title: '帮助'
  }
},
{
  name: 'help/view/:id',
  component: () => import('@/view/help/view'),
  meta: {
    title: ''
  }
},
{
  name: 'feedback',
  component: () => import('@/view/feedback'),
  meta: {
    title: '意见反馈'
  }
},
{
  name: 'feedback/:id',
  component: () => import('@/view/feedback/view'),
  meta: {
    title: '意见反馈'
  }
},
{
  name: 'page/privacy_policy',
  component: () => import('@/view/page/privacy_policy'),
  meta: {
    title: '隐私策略'
  }
},
{
  name: 'page/license',
  component: () => import('@/view/page/license'),
  meta: {
    title: '服务协议'
  }
},
{
  name: 'page/copyright',
  component: () => import('@/view/page/copyright'),
  meta: {
    title: '版权信息'
  }
},
{
  name: 'college',
  component: () => import('@/view/video/index'),
  meta: {
    title: '培训学院'
  }
},
{
  name: 'service/order/:id',
  component: () => import('@/view/service/order'),
  meta: {
    title: '在线客服'
  }
},
{
  name: 'default',
  component: () => import('@/view/default')
},
{
  name: 'error/:code',
  component: () => import('@/view/exception/error'),
  meta: {
    title: '错误提醒'
  }
},
{
  name: 'blank',
  component: () => import('@/view/default/blank')
},
{
  name: 'activity/new',
  component: () => import('@/view/activity/new'),
  meta: {
    title: '新品发布'
  }
},
{
  name: 'activity/care',
  component: () => import('@/view/activity/care'),
  meta: {
    title: '优越精选'
  }
},
{
  name: 'activity/member',
  component: () => import('@/view/activity/member'),
  meta: {
    title: '会员专区'
  }
},
{
  name: 'activity/hot',
  component: () => import('@/view/activity/hot'),
  meta: {
    title: '热销榜'
  }
},
{
  name: 'coupon',
  component: () => import('@/view/coupon'),
  meta: {
    title: '活动卡券'
  }
},
{
  name: 'strategy',
  component: () => import('@/view/strategy')
},
{
  name: 'train',
  component: () => import('@/view/train')
},
{
  name: 'share/:data',
  component: () => import('@/view/share'),
  meta: {
    title: '精彩分享'
  }
},
{
  path: '/wap',
  component: () => import('@/view/wap'),
  children: [{
    path: '/',
    redirect: 'index'
  }, {
    path: 'index',
    name: 'WapIndex',
    component: () => import('@/view/wap')
  }, {
    path: 'goods',
    name: 'WapGoods',
    component: () => import('@/view/wap/goods')
  }, {
    path: 'article',
    name: 'WapArticle',
    component: () => import('@/view/wap/article')
  }, {
    path: 'spread',
    name: 'WapSpread',
    component: () => import('@/view/wap/spread')
  }]
},
{
  name: 'download',
  component: () => import('@/view/download'),
  meta: {
    title: '优越定制APP'
  }
}
]

routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '')
})

const router = new Router({
  // mode: 'history',
  // base: process.env.assetsRoot,
  routes: routes
})

router.beforeEach((to, from, next) => {
  if (store.state.statusBarHeight === false && to.meta && to.meta.scheme && to.meta.scheme === 'Magic') {
    new Promise((resolve, reject) => {
      jsBridge.getClientLayout(res => {
        resolve(res)
      })
    }).then((data) => {
      store.state.clientLayout = data
      next()
    })
  } else {
    if (to.meta && to.meta.title) {
      document.title = to.meta.title
    }
    next()
  }
})

export default router
